package com.TP.www.service.impl;

import com.TP.www.dao.impl.StoreDAO;
import com.TP.www.po.Good;
import com.TP.www.po.Store;
import com.TP.www.service.StorePort;

import java.util.List;

import static com.TP.www.constant.Constant.*;

/**
 * @author 邱彦祥
 * 店铺功能实现类
 */
public class StoreImpl implements StorePort {
    StoreDAO storeDAO = new StoreDAO();
    /**
     * 创建店铺
     */
    @Override
    public void creatStore(){
        String storeName;
        String intro;
        int maxLength = 18;
        Integer id = loginUser.getId();
        System.out.println("您正在创建店铺:");
        System.out.println("请输入店铺名:");
        storeName = SYS_SC.nextLine();
        while(!(storeName.length() > 0 && storeName.length() <= maxLength)){
            System.out.println("输入有误!");
            System.out.println("请重新输入...");
            storeName = SYS_SC.nextLine();
        }
        System.out.println("请输入店铺简介:");
        intro = SYS_SC.nextLine();
        if(intro.length() == 0){
            intro = "店主很懒，啥都没留下咧....";
        }
        if(storeDAO.executeUpdate(CREAT_STORE_BY_MERCHANT_ID,storeName,intro,id)> 0){
            System.out.println("创建成功!");
        }else {
            System.out.println("创建失败...");
        }
        System.out.println("按任意键返回.....");
        SYS_SC.nextLine();
    }

    /**
     * 修改店铺信息
     */
    @Override
    public void modifyStore(){
        String storeName;
        String intro;
        int maxLength = 18;
        Integer id = loginUser.getId();
        Store store = storeDAO.executeQuery(SELECT_FROM_STORE_BY_MERCHANT_ID,id).get(0);
        System.out.println("*************当前店铺信息**************");
        System.out.println("店铺名: " + store.getStoreName());
        System.out.println("店铺简介: " + store.getIntro());
        System.out.println("*************************************");
        System.out.println("请输入新的店铺名:");
        storeName = SYS_SC.nextLine();
        while(!(storeName.length() > 0 && storeName.length() <= maxLength)){
            System.out.println("输入有误!");
            System.out.println("请重新输入...");
            storeName = SYS_SC.nextLine();
        }
        System.out.println("请输入新的店铺简介(直接回车则不做修改):");
        intro = SYS_SC.nextLine();
        if(intro.length() == 0){
            intro = store.getIntro();
        }
        if(storeDAO.executeUpdate(UPDATE_STORE_BY_MERCHANT_ID,storeName,intro,id) > 0){
            System.out.println("修改成功!");
        }else {
            System.out.println("修改失败...");
        }
        System.out.println("按任意键返回.....");
        SYS_SC.nextLine();
    }

    /**
     * 在店铺界面用户添加商品进购物车
     * @param goods 商品
     * @param i 商品种类个数
     */
    @Override
    public void storeAddCart(List<Good> goods, int i){
        String goodChoose;
        String goodNumChoose;
        System.out.println("请输入要购买商品序号(按0返回上级界面):");
        goodChoose = SYS_SC.nextLine();
        if(Integer.parseInt(goodChoose) == 0){
            return;
        }
        while (Integer.parseInt(goodChoose) >= i || Integer.parseInt(goodChoose) < 0){
            System.out.println("输入有误!");
            System.out.println("请重新输入商品序号(按0返回上级界面):");
            if(Integer.parseInt(goodChoose) == 0){
                return;
            }
        }
        System.out.println("请输入商品数量:");
        goodNumChoose = SYS_SC.nextLine();
        while (Integer.parseInt(goodChoose) <= 0 || Integer.parseInt(goodChoose) > GOOD_MAX_NUM){
            System.out.println("输入有误!");
            System.out.println("请重新输入数量:");
            goodChoose = SYS_SC.nextLine();
        }
        if(new CartImpl().add(loginUser,goods.get(Integer.parseInt(goodChoose)-1),Integer.parseInt(goodNumChoose))>0){
            System.out.println("加入购物车成功!");
        }
    }
}
